#!/usr/bin/python2.5
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Output svg/xml data for a marey graph
Drived from MareyGraph and overridden. No longer using objects
"""

from schedule_viewer import MareyGraph
from transitfeed_editor import *

class PatternGraph(MareyGraph):
  # TODO: Move functions of the class into javascript side 
  def getDraw(self, db, pattern_id, gaps, height):
    output = str()
    self._pattern_id = pattern_id
    self._db = db
    if not self._cache:
      self._gheight = height
      self._decorators = []
      self._stations = self._CalculateYLines(gaps)
      self._cache = "%s %s %s %s" % (self._DrawBox(),
                                     self._DrawHours(),
                                     self._DrawStations(),
                                     self._DrawTrips())

    output = "%s %s %s %s" % (self._DrawHeader(),
                              self._cache,
                              self._DrawDecorators(),
                              self._DrawFooter())
    return output

  def _DrawTrips(self, colpar=""):
    """Generates svg polylines for trips in the pattern."""

    stations = self._stations
    tmpstrs = []
    servlist = []
    for (trip_id, service_id, start_time) in PatternTrip.getTripsByPatternId(
      self._db, self._pattern_id):
      if not colpar:
        if service_id not in servlist:
          servlist.append(service_id)
        # Randomly generate a unique color for trips of different services
        shade = int(servlist.index(service_id) * (200 / len(servlist)) + 55)
        color = "#00%s00" %  hex(shade)[2:4]
      else:
        color = colpar

      start_offsets = [0]
      for j, freq_offset in enumerate(start_offsets):
        if j > 0 and not colpar:
          color = "purple"
        scriptcall = ('onmouseover="LineClick(\'%s\',\'Trip %s starting %s\')"'
          % (trip_id, trip_id, TimeStringToSeconds(start_time)))
        tmpstrhead = ('<polyline class="T" id="%s" stroke="%s" %s points="' %
          (str(trip_id), color, scriptcall))
        tmpstrs.append(tmpstrhead)

        for i, s in enumerate(Trip.GetStopTimesByTripId(
          self._db, trip_id)):
          arr_t = TimeStringToSeconds(s[0])
          dep_t = TimeStringToSeconds(s[1])
          if arr_t is None or dep_t is None:
            continue
          hour = self._hour_grid
          offset = self._offset
          arr_x = int(arr_t / 3600.0 * hour) - hour * offset
          dep_x = int(dep_t / 3600.0 * hour) - hour * offset
          tmpstrs.append("%s,%s " % (int(arr_x + 20), int(stations[i] + 20)))
          tmpstrs.append("%s,%s " % (int(dep_x + 20), int(stations[i] + 20)))
        tmpstrs.append('" />')
    return "".join(tmpstrs)
